package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import service.Service;

@SuppressWarnings("serial")
public class WegmatrixPanel extends JPanel {

  private int knotenanzahl;
  private JLabel[][] labels;

  public WegmatrixPanel(int knotenanzahl) {

    this.knotenanzahl = knotenanzahl;
    labels = new JLabel[knotenanzahl + 1][knotenanzahl + 1];

    init();

    setVisible(true);
  }


  public JLabel getLabels(int spalte, int zeile) {
    return labels[spalte][zeile];
  }






  private void init() {
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    setLayout(new BorderLayout());
    JPanel labelsPanel = new JPanel(new GridLayout(knotenanzahl+1, knotenanzahl+1));
    JLabel textLabel = new JLabel("Wegmatrix", JLabel.CENTER);
    textLabel.setFont(new Font("Serif", Font.BOLD, 20));
    textLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    
    add(textLabel, BorderLayout.NORTH);
    add(labelsPanel, BorderLayout.CENTER);
    
    
    for (int s = -1; s < knotenanzahl; s++) {
      for (int z = -1; z < knotenanzahl; z++) {
        
        if(z==-1 && s==-1)
          labelsPanel.add(new JLabel(""));
        else
         if(z == -1)
           labelsPanel.add(new JLabel((s+1)+"", JLabel.CENTER));
         else
        if(s == -1)
          labelsPanel.add(new JLabel((z+1)+"", JLabel.CENTER));
        else
        {
        
        labels[s][z] = new JLabel("0", JLabel.CENTER);
        labels[s][z].setBorder(BorderFactory.createLineBorder(Color.BLACK));
        labelsPanel.add(labels[s][z]);
        }
      }

    }
    
  }
  
  public void aktualisieren(){
    
	  
    int[][] wegmatrix = Service.getInstanz().berechneWegmatrix();
    
    for (int s = 0; s < knotenanzahl; s++) {
      for (int z = 0; z < knotenanzahl; z++) {
        labels[s][z].setText(String.valueOf(wegmatrix[s][z]));
        
        
        labels[s][z].setOpaque(true);       
        if(!labels[s][z].getText().equals("0"))
          labels[s][z].setBackground(Color.blue);
        else
          labels[s][z].setBackground(new JLabel().getBackground());
      }

    }

  }

}
